package com.example.orderservice.config;

import com.example.orderservice.model.Order;
import com.example.orderservice.repository.OrderRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Configuration
public class DataInitializer {

    private static final List<String> TECH_PRODUCTS = Arrays.asList(
            "智能手机 Pro",
            "无线耳机 Elite",
            "智能手表 X",
            "游戏笔记本",
            "4K显示器",
            "机械键盘",
            "电竞鼠标",
            "蓝牙音箱"
    );

    @Bean
    public CommandLineRunner initData(OrderRepository repository) {
        return args -> {
            Order order1 = new Order();
            order1.setOrderNumber("ORD-" + UUID.randomUUID().toString().substring(0, 8).toUpperCase());
            order1.setProductId(101L);
            order1.setProductName(TECH_PRODUCTS.get(0));
            order1.setQuantity(1);
            order1.setTotalAmount(new BigDecimal("8999.00"));
            order1.setOrderDate(LocalDateTime.now().minusDays(2));
            order1.setStatus("已完成");
            order1.setCustomerName("张伟");

            Order order2 = new Order();
            order2.setOrderNumber("ORD-" + UUID.randomUUID().toString().substring(0, 8).toUpperCase());
            order2.setProductId(205L);
            order2.setProductName(TECH_PRODUCTS.get(1));
            order2.setQuantity(2);
            order2.setTotalAmount(new BigDecimal("2598.00"));
            order2.setOrderDate(LocalDateTime.now().minusHours(5));
            order2.setStatus("处理中");
            order2.setCustomerName("李娜");

            Order order3 = new Order();
            order3.setOrderNumber("ORD-" + UUID.randomUUID().toString().substring(0, 8).toUpperCase());
            order3.setProductId(308L);
            order3.setProductName(TECH_PRODUCTS.get(2));
            order3.setQuantity(1);
            order3.setTotalAmount(new BigDecimal("2499.00"));
            order3.setOrderDate(LocalDateTime.now().minusDays(1));
            order3.setStatus("已发货");
            order3.setCustomerName("王强");

            repository.saveAll(Arrays.asList(order1, order2, order3));

            System.out.println("=== 订单数据初始化完成 ===");
            System.out.println("已生成 " + TECH_PRODUCTS.size() + " 种科技产品");
            System.out.println("已创建 3 条订单记录");
            System.out.println("最后更新时间: " + LocalDateTime.now());
        };
    }
}